/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package test.ashen;

import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.FrameGrabber.Exception;
import com.googlecode.javacv.VideoInputFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.googlecode.javacv.cpp.opencv_core.CvMat;
import com.googlecode.javacv.cpp.opencv_video.CvKalman;
import com.googlecode.javacv.cpp.opencv_calib3d.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import com.googlecode.javacv.cpp.opencv_calib3d.*;
import com.googlecode.javacv.cpp.opencv_core.CvMat;
import com.googlecode.javacv.cpp.opencv_video;
import com.googlecode.javacv.cpp.opencv_video.BackgroundSubtractorMOG2;
import com.sun.imageio.plugins.common.BogusColorSpace;
import com.googlecode.javacv.cpp.opencv_video.BackgroundSubtractorMOG2;
import java.awt.AWTException;
import java.awt.Robot;
import com.googlecode.javacv.cpp.opencv_calib3d.*;
import com.googlecode.javacv.FrameGrabber.Exception;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.googlecode.javacv.cpp.opencv_core.*;
import com.googlecode.javacv.*;
import com.googlecode.javacv.cpp.opencv_calib3d.*;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import test.shanika.CalculateHull;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import com.googlecode.javacv.cpp.opencv_video.BackgroundSubtractorMOG2;
import java.awt.AWTException;
import java.awt.Robot;
import com.googlecode.javacv.cpp.opencv_calib3d.*;
import com.googlecode.javacv.FrameGrabber.Exception;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.googlecode.javacv.cpp.opencv_core.*;
import com.googlecode.javacv.*;
import com.googlecode.javacv.cpp.opencv_calib3d.*;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import objectExtraction.HSVSkinDetector;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
/**
 *
 * @author Home
 */
public class bs_skin implements Runnable {

    public void run() {
        try {
            FrameGrabber gr = new VideoInputFrameGrabber(2);
            gr.start();
            Thread.sleep(1000);
            CameraSkin1 cs= new CameraSkin1(1);
            IplImage img=gr.grab();
            //IplImage bg = null;
            CanvasFrame cf = new CanvasFrame("sub");
            CanvasFrame cf2 = new CanvasFrame("skin");
            CanvasFrame cf3 = new CanvasFrame("and");
            HSVSkinDetector hs= new HSVSkinDetector();
            IplImage hsv;
            IplImage andi;
            if(img!=null){
                System.out.println("move");
                Thread.sleep(2000);
                //bg=gr.grab();
                System.out.println("done");
                IplImage fg=gr.grab();
                IplImage bg = cvCreateImage(fg.cvSize(), fg.depth(), fg.alphaChannel());
                andi=cvCreateImage(fg.cvSize(), fg.depth(), fg.alphaChannel());
                BackgroundSubtractorMOG2 bs= new opencv_video.BackgroundSubtractorMOG2(10,8, false);

                while(true){

                   bs.apply(fg, bg, -1);
                   cf.showImage(bg);
                   fg=gr.grab();
                   hsv=hs.getHSVSkin(fg);
                   cf2.showImage(hsv);
                   cvMul(bg, hsv, andi, 0);
                   cf3.showImage(andi);


                }


            }
        } catch (Exception ex) {
            Logger.getLogger(bs_skin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex) {
            Logger.getLogger(bs_skin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    
    public static void main(String[] args) {
        bs_skin bs= new bs_skin();
        Thread th= new Thread(bs);
        th.start();
    }


        private IplImage imopen(IplImage img,int i){
        IplConvKernel kernel=cvCreateStructuringElementEx(i, i,(int) Math.floor(i/2),(int) Math.floor(i/2), CV_SHAPE_ELLIPSE, null);
        IplImage open= cvCreateImage(cvGetSize(img), 8, 1);
        cvErode(img, open, kernel, 2);
        cvDilate(open, open, kernel, 2);
        return open;
    }

    private IplImage imclose(IplImage img,int i){
        IplConvKernel kernel=cvCreateStructuringElementEx(i, i,(int) Math.floor(i/2),(int) Math.floor(i/2), CV_SHAPE_ELLIPSE, null);
        IplImage close= cvCreateImage(cvGetSize(img), 8, 1);
        cvDilate(img, close, kernel, 2);
        cvErode(close, close, kernel, 2);
        return close;
    }

    private IplImage smooth(IplImage img,int i){
        IplImage mean= cvCreateImage(cvGetSize(img), 8, 1);
        cvSmooth(img, mean, i, i);
        return mean;
    }
}
